package taskTracker.ui.lite;

import java.util.Date;
import java.util.Enumeration;
import java.util.Vector;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.lcdui.StringItem;

import taskTracker.Project;
import taskTracker.ReportsManager;
import taskTracker.Tag;
import taskTracker.util.DrawingManager;
import taskTracker.util.Helper;

public class ReportViewer extends Form {

	/**A field that allows users to select beginning period of the report*/
	private final DateField startDate = new DateField("Start from", DateField.DATE);
	
	/**A field that allows users to select end period of the report*/
	private final DateField endDate = new DateField("Up to (not including)", DateField.DATE);
	
	/**Gives the user the ability to select a subset of tags*/
	private final ChoiceGroup cmbTags = new ChoiceGroup("Tags", ChoiceGroup.MULTIPLE);
	
	/**Allows user to limit to one project*/
	private final ChoiceGroup cmbProject = new ChoiceGroup("Project", ChoiceGroup.POPUP);
	
	/**Results go here*/
	private final StringItem txtReport = new StringItem("Report", "");
	private final ImageItem imgChart = new ImageItem("Chart", null, ImageItem.LAYOUT_CENTER, "");
	
	/**The command that returns back*/
	public final Command cmdBack = new Command("Back", Command.BACK, 1);
	
	/**A command that allows user to referesh the report*/
	public final Command cmdRefresh = new Command("Refresh", Command.SCREEN, 2);
	
	/**
	 * Creates a new report with the given title.
	 * @param title
	 */
	public ReportViewer(String title, Vector projects) {
		super(title);
		append(startDate);
		append(endDate);
		if (Tag.getAllTags().hasMoreElements()) {
			// Fill the combo box with all tags in the system
			for (Enumeration e = Tag.getAllTags();e.hasMoreElements();) {
				cmbTags.append(e.nextElement(), null);
			}
			append(cmbTags);
		}
		append(cmbProject);
		append(txtReport);
		append(imgChart);
		
		this.addCommand(cmdBack);
		this.addCommand(cmdRefresh);
		
		// Fill the combo box with all projects in system
		cmbProject.append((Object)"All projects", null);
		for (Enumeration e = projects.elements();e.hasMoreElements();) {
			cmbProject.append(e.nextElement(), null);
		}
		
		
		refreshReport();
	}
	
	public boolean refreshReport() {
		if (startDate.getDate() == null || endDate.getDate() == null)
			return false;
		// Get report criteria from GUI
		Tag[] selectedTags = new Tag[cmbTags.getSelectedCount()];
		cmbTags.getSelectedObjects(selectedTags);
		Vector vec = new Vector(selectedTags.length);
		for (int i=0;i<selectedTags.length;i++) {
			vec.addElement(selectedTags[i]);
		}
		Object selected = cmbProject.getSelectedObject();
		// Get report data
		Enumeration e = ReportsManager.instance.getTimeSummary(startDate.getDate().getTime(),
				endDate.getDate().getTime(), selected instanceof String ? null : (Project)selected, vec);
		String report = "";
		while (e.hasMoreElements()) {
			Object x = e.nextElement();
			if (x instanceof ReportsManager.ProjectTimeSummary) {
				ReportsManager.ProjectTimeSummary p = (ReportsManager.ProjectTimeSummary) x;
				report += p.project.getName() + ":" + Helper.formatTime(p.duration)+"\n";
			} else if (x instanceof ReportsManager.TaskTimeSummary) {
				ReportsManager.TaskTimeSummary p = (ReportsManager.TaskTimeSummary) x;
				report += p.task.getName() + ":" + Helper.formatTime(p.duration)+"\n";
			}
		}
		this.txtReport.setText(report);
		
		// Generate image report
		Enumeration e2 = ReportsManager.instance.getTimeSummary(startDate.getDate().getTime(),
				endDate.getDate().getTime(), selected instanceof String ? null : (Project)selected, vec);
		Image chart = Image.createImage(super.getWidth(), super.getWidth());
		//TODO elkharashy
		DrawingManager.getInstance().drawPie(chart.getGraphics(), chart.getWidth(), e2);
		imgChart.setImage(chart);
		
		return true;
	}

	/**
	 * Prepares the report with the given data.
	 * @param start - Start date
	 * @param end - End date
	 * @param project - Project. If this is <code>null</code>, it gives summary of all projects.
	 */
	public void prepare(long start, long end, Project project) {
		startDate.setDate(new Date(start));
		endDate.setDate(new Date(end));
		if (project == null) {
			cmbProject.setSelectedIndex(0, true);
		} else {
			cmbProject.setselectedObject(project);
		}
	}

}
